/*
 * @Author: liutongchao 1923236391@qq.com
 * @Date: 2023-04-27 14:49:28
 * @LastEditors: liutongchao 1923236391@qq.com
 * @LastEditTime: 2025-02-25 15:25:55
 * @FilePath: \greenbeeadmin\middleware\AdminCheckLogin.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package middleware

import (
	greenbee "greenbeeadmin/controllers/greenbee"
	greenbeeUtils "greenbeeadmin/controllers/greenbee/utils"
	"net/http"
	"strings"

	beego "github.com/beego/beego/v2/adapter"
	"github.com/beego/beego/v2/adapter/context"
)

/**
 * @description: 安装验证 中间件
 * @param {[]string} letGoUrlItem 全局放行路由
 * @return {*}
 */
func CheckInstalled(letGoUrlItem []string) {

	//安装验证 中间件过滤器
	var instelled = func(ctx *context.Context) {
		//fmt.Println("安装验证中间件")
		requestPath := ctx.Request.URL.Path
		requestPath = strings.Replace(requestPath, ".html", "", -1)
		// 放行设置
		//letGoUrlItem := []string{}

		if !greenbeeUtils.InArrayStr(requestPath, letGoUrlItem) {
			// 系统是否安装验证
			if !greenbee.IsInstalled() {
				// 跳转,方式：301(永久移动),308(永久重定向),307(临时重定向)
				ctx.Redirect(http.StatusFound, "/install")
				return
			}
		}
	}

	// 过滤器
	beego.InsertFilter("/*", beego.BeforeRouter, instelled)
}
